package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.ForbidRule;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IForbidRuleDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IForbidRuleService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.dto.ForbidRuleDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 禁发时段规则管理 接口实现类
 * @author wuluodan
 *
 */
@Service
public class ForbidRuleService implements IForbidRuleService {

	@Autowired
	private IForbidRuleDao forbidRuleDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<ForbidRuleDTO> selectPage(QueryPage<ForbidRule> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("status", page.getParams().get("status"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<ForbidRuleDTO> result = forbidRuleDao.selectPage(map);
		int totalCount = forbidRuleDao.selectCount(map);
		
		QueryPage<ForbidRuleDTO> queryPage = new QueryPage<ForbidRuleDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public ForbidRule selectByPrimaryKey(String forbidRuleId) {
		return forbidRuleDao.selectByPrimaryKey(forbidRuleId);
	}

	@Override
	public ForbidRuleDTO toUpdate(String forbidRuleId) {
		ForbidRule entity = this.selectByPrimaryKey(forbidRuleId);
		ForbidRuleDTO forbidRuleDTO = new ForbidRuleDTO(entity);
		return forbidRuleDTO;
	}

	@Override
	public void changeStatus(ForbidRule entity) {
		this.update(entity);
	}

	@Override
	public void save(ForbidRule entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		forbidRuleDao.insertSelective(entity);
		
		this.updateParamRec("保存禁发时段规则");
	}

	@Override
	public void update(ForbidRule entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		forbidRuleDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新禁发时段规则");
	}

	@Override
	public void delete(String forbidRuleId) {
		forbidRuleDao.deleteByPrimaryKey(forbidRuleId);
		this.updateParamRec("删除禁发时段规则");
	}

	@Override
	public List<ForbidRuleDTO> getRecord(Map<String, Object> params) {
		List<ForbidRuleDTO> list = forbidRuleDao.getRecord(params);
		return list;
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.FORBID_RULE, desc);
	}

}
